import api from './api';
import type { Soup, CreateSoupRequest, LeaderboardEntry, Achievement, UserAchievement } from '../types';

// 内容服务
const contentService = {
  // 创建海龟汤
  createSoup: async (data: CreateSoupRequest): Promise<Soup> => {
    const response = await api.post('/soups', data);
    return response;
  },

  // 获取我的海龟汤列表
  getMySoups: async (): Promise<Soup[]> => {
    const response = await api.get('/soups/my');
    return response;
  },

  // 获取海龟汤详情
  getSoup: async (soupId: string): Promise<Soup> => {
    const response = await api.get(`/soups/${soupId}`);
    return response;
  },

  // 更新海龟汤
  updateSoup: async (soupId: string, data: Partial<CreateSoupRequest>): Promise<Soup> => {
    const response = await api.put(`/soups/${soupId}`, data);
    return response;
  },

  // 删除海龟汤
  deleteSoup: async (soupId: string): Promise<void> => {
    await api.delete(`/soups/${soupId}`);
  },

  // 提交海龟汤审核
  submitForReview: async (soupId: string): Promise<Soup> => {
    const response = await api.post(`/soups/${soupId}/review`);
    return response;
  },

  // 搜索海龟汤
  searchSoups: async (params: { keyword?: string; difficulty?: string; theme?: string; status?: string }): Promise<Soup[]> => {
    const response = await api.get('/soups/search', {
      params
    });
    return response;
  },

  // 获取排行榜
  getLeaderboard: async (type: string, limit = 50, offset = 0): Promise<LeaderboardEntry[]> => {
    const response = await api.get('/leaderboard', {
      params: { type, limit, offset }
    });
    return response;
  },

  // 获取我的排行榜位置
  getMyRank: async (type: string): Promise<{ rank: number }> => {
    const response = await api.get('/leaderboard/my', {
      params: { type }
    });
    return response;
  },

  // 获取所有成就列表
  getAchievements: async (): Promise<Achievement[]> => {
    const response = await api.get('/achievements');
    return response;
  },

  // 获取我的成就
  getMyAchievements: async (): Promise<UserAchievement[]> => {
    const response = await api.get('/achievements/my');
    return response;
  },

  // 获取成就详情
  getAchievementDetail: async (achievementId: string): Promise<Achievement> => {
    const response = await api.get(`/achievements/${achievementId}`);
    return response;
  },

  // 收藏海龟汤
  favoriteSoup: async (soupId: string): Promise<void> => {
    await api.post(`/soups/${soupId}/favorite`);
  },

  // 取消收藏海龟汤
  unfavoriteSoup: async (soupId: string): Promise<void> => {
    await api.post(`/soups/${soupId}/unfavorite`);
  },

  // 获取我的收藏
  getMyFavorites: async (): Promise<Soup[]> => {
    const response = await api.get('/soups/favorites');
    return response;
  },

  // 对海龟汤进行评分
  rateSoup: async (soupId: string, rating: number): Promise<void> => {
    await api.post(`/soups/${soupId}/rate`, { rating });
  }
};

export default contentService;